jQuery(document).ready(function ($) {

    function show(h){
        $('.msg').html(h);
    }

    function hide(){
        $('.msg').attr('class', 'msg').html('');
    }

    $('input').focus(hide)

    $('#login').click(function(){
        var username = $('#username').val();
        var password = $('#password').val();
        var captcha = $('#captcha').val();

        if (username == ''){
            show("用户名不能为空");return;
        }

        if (password == ''){
            show("密码不能为空");return;
        }
        if (captcha == ''){
            show("请输入验证码");return;
        }

        $.ajax({
            type: "post",
            url: config.submit_url,
            data: {username:username, password:password,captcha:captcha},
            dataType: "json",
            success: function(e){
                if (e.code === 0){
                    show(e.msg)
                    setTimeout(function () {
                        location.href = config.home_url
                    }, 800);
                }else{
                    show(e.msg)
                    $('#captcha_img').attr('src','/captcha.html?tm='+Math.random());
                    $("#captcha").val('')
                }
            }
        });

    });

});